package com.gack.business.service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gack.business.dao.BusinessCardRecordDao;
import com.gack.business.dao.EnterpriseDao;
import com.gack.business.dao.StoresDao;
import com.gack.business.repository.DepartmentRepository;
import com.gack.business.repository.EnterpriseRepository;
import com.gack.business.vo.CertificationNotPassedEnterpriseDetailVO;
import com.gack.business.vo.CertificationPassedEnterpriseDetailVO;
import com.gack.business.vo.CertificationWaitingEnterpriseDetailVO;
import com.gack.business.vo.DepartmentItemVO;
import com.gack.business.vo.EnterpriseBasicInfoVO;
import com.gack.business.vo.EnterpriseFrameworkVO;
import com.gack.business.vo.UserItemVO;
import com.gack.helper.common.util.ChangeDateTimeUtil;
import com.gack.helper.common.util.Pinyin;

@Transactional
@Service
public class EnterpriseOperatorService implements EnterpriseOperatorServiceInterface {
	
	@Autowired
	private EnterpriseRepository enterpriseRepository;
	@Autowired
	private EnterpriseDao enterpriseDao;
	@Autowired
	private DepartmentRepository departmentRepository;
	@Autowired
	private BusinessCardRecordDao businessCardRecordDao;
	@Autowired
	private StoresDao storesDao;
	@Autowired
	private BackLogServiceInterface backLogService;
	
	/**
	 * 根据筛选条件,查询公司ItemVO集合
	 * @param page 所请求的页数  从0开始
	 * @param size 每页显示记录数量
	 * @param status 公司认证状态 0:未认证 1:已认证
	 * @param secondCondition 第二个筛选条件 1:公司名称 2:创建人名称 3:所属行业 4:所在地区
	 * @param input 输入匹配串
	 * @return status:error/success msg:错误或成功消息  
	 * 		   registeredNum:已注册公司数量  certificationNum:已认证公司数量  ucertificationNum:未认证公司数量  dissolveNum:已解散公司数量   payAmount:购买商务卡的共计消费金额
	 * 		   enterpriseItemList:公司ItemVO集合(按注册时间降序排序) total:总页数记录数
	 * 		   certificationWaitingEnterpriseNum:待认证审批的公司数量
	 */
	@Override
	public Map<String, Object> findEnterpriseItemVOListByInput(Integer page, Integer size, String status, String secondCondition, String input) {
		Map<String, Object> map = new HashMap<>();
		
		if(!"0".equals(status) && !"1".equals(status)){
			map.put("status", "error");
			map.put("msg", "公司认证状态不正确");
			return map;
		}
		
		if(!"1".equals(secondCondition) && !"2".equals(secondCondition) && !"3".equals(secondCondition) && !"4".equals(secondCondition)){
			map.put("status", "error");
			map.put("msg", "筛选条件不正确");
			return map;
		}
		
		//获取已注册公司数量、已认证公司数量、未认证公司数量、已解散公司数量、购买商务卡的共计消费金额
		List<Object[]> statisticsList = enterpriseRepository.findStatistics();
		String registeredNum = null;
		String certificationNum = null;
		String ucertificationNum = null;
		String dissolveNum = null;
		String payAmount = null;
		
		for(Object[] t : statisticsList){
			switch((String)t[1]){
				case "registered":
					registeredNum = String.valueOf((BigInteger)t[0]);break;
				case "certification":
					certificationNum = String.valueOf((BigInteger)t[0]);break;
				case "ucertification":
					ucertificationNum = String.valueOf((BigInteger)t[0]);break;
				case "dissolve":
					dissolveNum = String.valueOf((BigInteger)t[0]);break;
				case "payAmount":
					payAmount = String.valueOf((BigInteger)t[0]);break;
			}
		}
		
		//按筛选条件筛选公司ItemVO
		Map<String, Object> enterpriseMap = null;
		switch(secondCondition){
			case "1":enterpriseMap = enterpriseDao.findEnterpriseItemVOListByStatusAndName(page, size, status, input);break;
			case "2":enterpriseMap = enterpriseDao.findEnterpriseItemVOListByStatusAndCreatorName(page, size, status, input);break;
			case "3":enterpriseMap = enterpriseDao.findEnterpriseItemVOListByStatusAndIndustry(page, size, status, input);break;
			case "4":enterpriseMap = enterpriseDao.findEnterpriseItemVOListByStatusAndLocation(page, size, status, input);break;
			default: map.put("status", "error");
					 map.put("msg", "传入的第二个筛选条件不正确");
					 return map;
		}
		
		//certificationWaitingEnterpriseNum:待认证审批的公司数量
		int certificationWaitingEnterpriseNum = enterpriseRepository.countEnterpriseNumByStatus("2");
		
		map.put("status", "success");
		map.put("msg", "操作成功");
		map.put("registeredNum", registeredNum);
		map.put("certificationNum", certificationNum);
		map.put("ucertificationNum", ucertificationNum);
		map.put("dissolveNum", dissolveNum);
		map.put("payAmount", payAmount);
		map.put("total", enterpriseMap.get("total"));
		map.put("enterpriseItemList", enterpriseMap.get("enterpriseItemList"));
		map.put("certificationWaitingEnterpriseNum", certificationWaitingEnterpriseNum);
		
		return map;
	}

	/**
	 * 根据公司id,查询公司基本信息(enterpriseBasicInfo),公司架构图(enterpriseFramework), 公司某部门人员信息ItemVO集合(userItemList),
	 * 			公司的购卡记录ItemVO集合(businessCardRecordItemList)
	 * 			公司的消费明细ItemVO集合(orderItemList)
	 * @param enterpriseId
	 * @return status:error/success msg:错误或成功消息
	 *   	   enterpriseBasicInfo:公司基本信息对象 
	 *         enterpriseFramework:公司架构对象 
	 *         topDepartmentItemList:公司一级部门对象集合(按部门名称排序)(若有无部门人员则展示'无部门',添加到部门集合最末处)
	 *         userItemList:公司人员信息ItemVO集合(前10条记录,按部门名称+人员名称排序)   userItemListTotal:公司人员信息ItemVO集合总数量
	 *         businessCardRecordItemList:购卡记录集合   businessCardRecordItemListTotal:公司购卡记录总数量 
	 *         orderItemList:匹配筛选条件的消费记录集合   orderItemListTotal:匹配筛选条件的全部消费记录数量 
	 *         enterpriseSurplusAmount:公司账户余额
	 */
	@Override
	public Map<String, Object> findEnterpriseDetailByEnterpriseId(String enterpriseId){
		Map<String, Object> map = new HashMap<>();
		
		//查询公司基本信息
		Map<String, Object> enterpriseBasicInfoMap = findEnterpriseBasicInfoByEnterpriseId(enterpriseId);
		if(enterpriseBasicInfoMap.get("status").equals("error")){
			enterpriseBasicInfoMap.put("mark", "enterpriseBasicInfo");
			return enterpriseBasicInfoMap;
		}
		
		//查询公司架构图
		Map<String, Object> enterpriseFrameworkMap = findEnterpriseFramework(enterpriseId);
		if(enterpriseBasicInfoMap.get("status").equals("error")){
			enterpriseBasicInfoMap.put("mark", "enterpriseFramework");
			return enterpriseFrameworkMap;
		}
		
		//topDepartmentItemList:公司一级部门对象集合(按部门名称排序)(若有无部门人员则展示'无部门',添加到部门集合最末处)
		Map<String, Object> topDepartmentItemListMap = findTopDepartmentByEnterpriseId(enterpriseId);
		if(topDepartmentItemListMap.get("status").equals("error")){
			topDepartmentItemListMap.put("mark", "topDepartmentItemListMap");
			return map;
		}
		
		//查询公司某部门人员信息ItemVO集合(查询该公司排在第一位的一级部门的人员信息ItemVO集合)
		String departmentId = ((List<DepartmentItemVO>)topDepartmentItemListMap.get("topDepartmentItemList")).get(0).getId();
		Map<String, Object> userItemListMap = findUserItemListByEnterpriseIdAndDepartmentId(enterpriseId, departmentId);
		if(userItemListMap.get("status").equals("error")){
			userItemListMap.put("mark", "userItemList");
			return userItemListMap;
		}
		List<UserItemVO> userItemList = (List<UserItemVO>)userItemListMap.get("userItemList");
		
		//查询公司的购卡记录
		Map<String, Object> businessCardRecordItemListMap = findBusinessCardRecordByEnterpriseId(0, 10, enterpriseId);
		if(businessCardRecordItemListMap.get("status").equals("error")){
			businessCardRecordItemListMap.put("mark", "businessCardRecordItemList");
			return businessCardRecordItemListMap;
		}
		
		//查询公司的消费明细
		Map<String, Object> orderItemListMap = findOrderItemByEnterpriseId(0, 10, null, null, null, null, enterpriseId);
		if(orderItemListMap.get("status").equals("error")){
			orderItemListMap.put("mark", "orderItemList");
			return orderItemListMap;
		}
		
		
		map.put("status", "success");
		map.put("msg", "操作成功");
		map.put("enterpriseBasicInfo", enterpriseBasicInfoMap.get("enterpriseBasicInfo"));
		map.put("enterpriseFramework", enterpriseFrameworkMap.get("enterpriseFramework"));
		map.put("topDepartmentItemList", topDepartmentItemListMap.get("topDepartmentItemList"));
		map.put("userItemList", userItemList.size()>=10?userItemList.subList(0, 10):userItemList.subList(0, userItemList.size()));
		map.put("userItemListTotal", userItemList.size());
		map.put("businessCardRecordItemList", businessCardRecordItemListMap.get("businessCardRecordItemList"));
		map.put("businessCardRecordItemListTotal", businessCardRecordItemListMap.get("total"));
		map.put("orderItemList", orderItemListMap.get("orderItemList"));
		map.put("orderItemListTotal", orderItemListMap.get("total"));
		map.put("enterpriseSurplusAmount", enterpriseRepository.findSurplusAmountByEnterpriseId(enterpriseId));
		
		return map;
	}
	
	/**
	 * 根据公司id,查询公司基本信息
	 * @param enterpriseId 公司id
	 * @return status:error/success msg:错误或成功消息  enterpriseBasicInfo:公司基本信息对象
	 */
	@Override
	public Map<String, Object> findEnterpriseBasicInfoByEnterpriseId(String enterpriseId){
		Map<String, Object> map = new HashMap<>();
		
		if(enterpriseId == null || enterpriseId.length() == 0){
			map.put("status", "error");
			map.put("msg", "公司为空");
			return map;
		}
		
		EnterpriseBasicInfoVO enterpriseBasicInfo = enterpriseDao.findEnterpriseBasicInfoByEnterpriseId(enterpriseId);
		if(enterpriseBasicInfo == null){
			map.put("status", "error");
			map.put("msg", "查无此公司");
			return map;
		}
		
		map.put("status", "success");
		map.put("msg", "操作成功");
		map.put("enterpriseBasicInfo", enterpriseBasicInfo);
		return map;
	}
	
	/**
	 * 根据公司id,查询公司架构图
	 * @param enterpriseId 公司id
	 * @return status:error/success msg:错误或成功消息  enterpriseFramework:公司架构对象
	 */
	@Override
	public Map<String, Object> findEnterpriseFramework(String enterpriseId) {
		Map<String, Object> map = new HashMap<>();
		
		if(enterpriseId == null || enterpriseId.length() == 0){
			map.put("status", "error");
			map.put("msg", "公司为空");
			return map;
		}
		
		EnterpriseFrameworkVO enterpriseFramework = enterpriseRepository.findEnterpriseFrameworkVO(enterpriseId);
		if(enterpriseFramework == null || enterpriseFramework.getId() == null || enterpriseFramework.getId().equals("") ){
			map.put("status", "error");
			map.put("msg", "查无此公司");
			return map;
		}
		
		//获取该公司下部门信息
		List<EnterpriseFrameworkVO.DepartmentBasicInfo> departmentBasicInfoList = departmentRepository.findDepartmentBasicInfoByEnterpriseId(enterpriseId);
		
		//部门按名称排序
		Comparator<EnterpriseFrameworkVO.DepartmentBasicInfo> comparator = new Comparator<EnterpriseFrameworkVO.DepartmentBasicInfo>(){
			@Override
			public int compare(EnterpriseFrameworkVO.DepartmentBasicInfo d1, EnterpriseFrameworkVO.DepartmentBasicInfo d2) {
				String s1 = Pinyin.getStringPinYin(d1.getName());
				String s2 = Pinyin.getStringPinYin(d2.getName());
				return s1.compareTo(s2);
			}
		};
		Collections.sort(departmentBasicInfoList, comparator);
		
		for(EnterpriseFrameworkVO.DepartmentBasicInfo t : departmentBasicInfoList){
			for(EnterpriseFrameworkVO.DepartmentBasicInfo t1 : departmentBasicInfoList){
				if(t1.getId().equals(t.getParentId())){
					t1.getChildren().add(t);
					break;
				}
			}
		}
		
		//向enterpriseFramework对象中注入一级部门
		for(EnterpriseFrameworkVO.DepartmentBasicInfo t : departmentBasicInfoList){
			if(t.getParentId().equals("0")){
				enterpriseFramework.getChildren().add(t);
			}
		}
		
		//若存在无部门人员,则显示无部门人数
		int noDepartmentNum = enterpriseRepository.countNoDepartmentNumByEnterpriseId(enterpriseId);
		EnterpriseFrameworkVO.DepartmentBasicInfo noDepartmentBasicInfo = new EnterpriseFrameworkVO.DepartmentBasicInfo();
		noDepartmentBasicInfo.setId("-1");//设置无部门id
		noDepartmentBasicInfo.setName("无部门");//设置无部门名称
		noDepartmentBasicInfo.setNumber(noDepartmentNum);//设置无部门人员数量
		enterpriseFramework.getChildren().add(noDepartmentBasicInfo);
		
		map.put("status", "success");
		map.put("msg", "操作成功");
		map.put("enterpriseFramework", enterpriseFramework);
		
		return map;
	}
	
	/**
	 * 根据公司id,查询该公司所有一级部门ItemVO(若有无部门人员,则显示'无部门'选项)
	 * @param enterpriseId 公司id
	 * @return status:error/success msg:错误或成功消息  topDepartmentItemList:一级部门ItemVO集合
	 */
	@Override
	public Map<String, Object> findTopDepartmentByEnterpriseId(String enterpriseId){
		Map<String, Object> map = new HashMap<>();
		
		if(enterpriseId == null || enterpriseId.length() == 0){
			map.put("status", "error");
			map.put("msg", "公司为空");
			return map;
		}
		
		List<DepartmentItemVO> topDepartmentItemList = departmentRepository.findTopDepartmentItemVOByEnterpriseId(enterpriseId);
		//部门按名称排序
		Comparator<DepartmentItemVO> comparator = new Comparator<DepartmentItemVO>(){
			@Override
			public int compare(DepartmentItemVO d1, DepartmentItemVO d2) {
				String s1 = Pinyin.getStringPinYin(d1.getName());
				String s2 = Pinyin.getStringPinYin(d2.getName());
				return s1.compareTo(s2);
			}
		};
		Collections.sort(topDepartmentItemList, comparator);
		
		//判断是否有无部门人员,若有则加入无部门项
		int noDepartmentNum = enterpriseRepository.countNoDepartmentNumByEnterpriseId(enterpriseId);
		if(noDepartmentNum != 0){
			DepartmentItemVO t = new DepartmentItemVO();
			t.setId("-1");
			t.setName("无部门");
			t.setNumber(noDepartmentNum);
			topDepartmentItemList.add(t);
		}
		
		map.put("status", "success");
		map.put("msg", "操作成功");
		map.put("topDepartmentItemList", topDepartmentItemList);
		
		return map;
	}
	
	/**
	 * 查询某部门下所有子部门id(包括子子部门id)
	 * @param departmentId 部门id
	 * @return status:error/success msg:错误或成功消息  subDepartmentIdList:子部门id集合
	 */
	@Override
	public Map<String, Object> findAllSubDepartmentId(String departmentId) {
		Map<String, Object> map = new HashMap<>();
	
		if(departmentId == null || departmentId.length() == 0){
			map.put("status", "error");
			map.put("msg", "部门为空");
			return map;
		}
	
		List<String> subDepartmentIdList = new ArrayList<>();//存储所有子部门id
		List<String> storeDepartmentIdList = new ArrayList<>();//存储所有未查询过子部门的部门id集合
		storeDepartmentIdList.add(departmentId);
		
		while(storeDepartmentIdList.size() != 0){
			Iterator<String> it = storeDepartmentIdList.iterator();
			if(it.hasNext()){
				List<String> t = departmentRepository.findNextSubDepartmentIdByDepartmentId(it.next());
				it.remove();
				storeDepartmentIdList.addAll(t);
				subDepartmentIdList.addAll(t);
			}
		}
		
		map.put("status", "success");
		map.put("msg", "操作成功");
		map.put("subDepartmentIdList", subDepartmentIdList);
		
		return map;
	}
	
	/**
	 * 根据部门id,查询部门名称  规则:(一级部门_二级部门_三级部门)
	 * @param departmentId 部门id
	 * @return status:error/success msg:错误或成功消息  departmentName:部门名称
	 */
	public Map<String, Object> findDepartmentName(String departmentId){
		Map<String, Object> map = new HashMap<>();
		
		if(departmentId == null || departmentId.length() == 0 || departmentId.equals("0")){
			map.put("status", "error");
			map.put("msg", "部门为空");
			return map;
		}
		
		StringBuilder name = new StringBuilder();
		String parentId = departmentId;
		while(!parentId.equals("0")){
			List<Object[]> tList = departmentRepository.findNameAndParentIdByDepartmentId(departmentId);
			if(tList.size() > 0){
				parentId = (String)tList.get(0)[1];
				name.insert(0, "_" + (String)tList.get(0)[0]);
			}
		}
		
		if(name.length() != 0){
			name = new StringBuilder(name.substring("_".length(), name.length()));
		}
		
		map.put("status", "success");
		map.put("msg", "操作成功");
		map.put("departmentName", name.toString());
		return map;
	}
	
	/**
	 * 根据公司id和部门id查询某部门下所有人员ItemVO 按部门名称+人员名称排序
	 * @param enterpriseId 公司id
	 * @param departmentId 一级部门id,若id为-1,则显示无部门人员信息
	 * @return  status:error/success msg:错误或成功消息  userItemList:人员ItemVO集合
	 */
	@Override
	public Map<String, Object> findUserItemListByEnterpriseIdAndDepartmentId(String enterpriseId, String departmentId){
		Map<String, Object> map = new HashMap<>();
		
		if(enterpriseId == null || enterpriseId.length() == 0){
			map.put("status", "error");
			map.put("msg", "公司为空");
			return map;
		}
		if(departmentId == null || departmentId.length() == 0 || departmentId.equals("0")){
			map.put("status", "error");
			map.put("msg", "部门为空");
			return map;
		}
		
		//若departmentId为-1,则查询无部门人员信息
		if(departmentId.equals("-1")){
			List<UserItemVO> userItemVOList = enterpriseDao.findNoDepartmentUserItemListByEnterpriseId(enterpriseId);
			//对无部门人员排序
			Comparator<UserItemVO> comparator = new Comparator<UserItemVO>(){
				@Override
				public int compare(UserItemVO d1, UserItemVO d2) {
					String s1 = Pinyin.getStringPinYin(d1.getNameForShow());
					String s2 = Pinyin.getStringPinYin(d2.getNameForShow());
					return s1.compareTo(s2);
				}
			};
			Collections.sort(userItemVOList, comparator);
			
			map.put("status", "success");
			map.put("msg", "操作成功");
			map.put("userItemList", userItemVOList);
			return map;
		}
		
		//若departmentId不为-1,则查询相应部门人员信息(包含子部门、子子部门的人员信息,且不包括已被删除部门)
		Map<String, Object> tMap = findAllSubDepartmentId(departmentId);
		if(tMap.get("status").equals("error")){
			tMap.put("mark", "findAllSubDepartmentId");
			return tMap;
		}
		
		List<String> subDepartmentIdList = (List<String>)tMap.get("subDepartmentIdList");
		subDepartmentIdList.add(departmentId);
		List<UserItemVO> userItemVOList = enterpriseDao.findUserItemListByEnterpriseIdAndDepartmentId(enterpriseId, subDepartmentIdList);
		//设置游离状态的UserItemVO
		List<UserItemVO> userItemVOListNotEntity = new ArrayList<>();
		for(UserItemVO t : userItemVOList){
			UserItemVO temp = new UserItemVO();
			temp.setId(t.getId());
			temp.setUsername(t.getUsername());
			temp.setPortrait(t.getPortrait());
			temp.setNameForShow(t.getNameForShow());
			temp.setDepartmentId(t.getDepartmentId());
			temp.setPosition(t.getPosition());
			userItemVOListNotEntity.add(temp);
		}
		
		//设置部门名称(一级部门_二级部门_三级部门)
		for(UserItemVO t : userItemVOListNotEntity){
			Map<String, Object> tMap1 = findDepartmentName(t.getDepartmentId());
			if(tMap1.get("status").equals("error")){
				tMap1.put("mark", "findDepartmentName");
				return tMap1;
			}
			t.setDepartmentName((String)tMap1.get("departmentName"));
		}
		
		//对该部门下人员排序
		Comparator<UserItemVO> comparator = new Comparator<UserItemVO>(){
			@Override
			public int compare(UserItemVO d1, UserItemVO d2) {
				String s1 = Pinyin.getStringPinYin(d1.getDepartmentName() + "_" + d1.getNameForShow());
				String s2 = Pinyin.getStringPinYin(d2.getDepartmentName() + "_" + d2.getNameForShow());
				return s1.compareTo(s2);
			}
		};
		Collections.sort(userItemVOListNotEntity, comparator);
		
		map.put("status", "success");
		map.put("msg", "操作成功");
		map.put("userItemList", userItemVOListNotEntity);
		
		return map;
	}
	
	/**
	 * 根据公司id,查询某公司的购卡记录(已支付的记录)(若page和size都为-1,则为全部查找;否则为分页查找) 按购买日期降序排序
	 * @param page 请求第几页  从0开始   
	 * @param size 每页显示数量
	 * @param enterpriseId 公司id
	 * @return status:error/success msg:错误或成功消息  businessCardRecordItemList:购卡记录集合   total:全部购卡记录数量 
	 */
	@Override
	public Map<String, Object> findBusinessCardRecordByEnterpriseId(Integer page, Integer size, String enterpriseId){
		Map<String, Object> map = new HashMap<>();
		
		if(enterpriseId == null || enterpriseId.length() == 0){
			map.put("status", "error");
			map.put("msg", "公司为空");
			return map;
		}
		
		if(page == null || size == null){
			map.put("status", "error");
			map.put("msg", "分页为空");
			return map;
		}
		
		Map<String, Object> businessCardRecordMap = businessCardRecordDao.findBusinessCardRecordItemVOByEnterpriseId(page, size, enterpriseId);
		map.put("status", "success");
		map.put("msg", "操作成功");
		map.put("businessCardRecordItemList", businessCardRecordMap.get("businessCardRecordItemList"));
		map.put("total", businessCardRecordMap.get("total"));
		return map;
	}
	
	/**
	 * 根据公司id和筛选条件,查询消费记录(状态为:用户主动支付订单)(若page和size都为-1,则为全部查找,且不考虑筛选条件;否则为分页查找,考虑筛选条件)
	 * @param page 请求第几页  从0开始   
	 * @param size 每页显示数量
	 * @param userNameForShow 筛选条件可选  用户名称  规则:若为已认证,则显示真实姓名,否则显示昵称
	 * @param departmentName 筛选条件可选  部门名称
	 * @param beginTime 筛选条件可选  会议开始时间
	 * @param endTime 筛选条件可选  会议结束时间
	 * @param enterpriseId 公司id
	 * @return status:error/success msg:错误或成功消息  orderItemList:匹配筛选条件的消费记录集合   total:匹配筛选条件的全部消费记录数量 
	 */
	@Override
	public Map<String, Object> findOrderItemByEnterpriseId(Integer page, Integer size, String userNameForShow, String departmentName, String beginTime, String endTime, String enterpriseId){
		Map<String, Object> map = new HashMap<>();
		
		if(enterpriseId == null || enterpriseId.length() == 0){
			map.put("status", "error");
			map.put("msg", "公司为空");
			return map;
		}
		
		if(page == null || size == null){
			map.put("status", "error");
			map.put("msg", "分页为空");
			return map;
		}
		
		if(beginTime != null && beginTime.length() != 0 && endTime != null && endTime.length() != 0){
			try {
				beginTime = ChangeDateTimeUtil.zExchangeUTC(beginTime);
				endTime = ChangeDateTimeUtil.zExchangeUTCAndAddDay(endTime, 1);
			} catch (Exception e) {
				e.printStackTrace();
				map.put("status", "error");
				map.put("msg", "日期转换错误");
				return map;
			}
		}
		
		Map<String, Object> orderItemListMap = storesDao.findOrderItemByEnterpriseId(page, size, userNameForShow, departmentName, beginTime, endTime, enterpriseId);
		map.put("status", "success");
		map.put("msg", "操作成功");
		map.put("orderItemList", orderItemListMap.get("orderItemList"));
		map.put("total", orderItemListMap.get("total"));
		return map;
	}
	
	/**
	 * 查询认证审核未通过的公司
	 * @param page 请求第几页  从0开始   
	 * @param size 每页显示数量
	 * @return status:error/success msg:错误或成功消息  certificationNotPassedEnterpriseItemList:认证审核未通过的公司ItemVO total:认证审核未通过的全部公司数量
	 */
	@Override
	public Map<String, Object> findCertificationNotPassedEnterpriseItemVO(Integer page, Integer size) {
		Map<String, Object> map = new HashMap<>();
		
		if(page == null || size == null){
			map.put("status", "error");
			map.put("msg", "分页为空");
			return map;
		}
		
		Map<String, Object> certificationNotPassedEnterpriseItemListMap = enterpriseDao.findCertificationNotPassedEnterpriseItemVO(page, size);
		map.put("status", "success");
		map.put("msg", "操作成功");
		map.put("certificationNotPassedEnterpriseItemList", certificationNotPassedEnterpriseItemListMap.get("certificationNotPassedEnterpriseItemList"));
		map.put("total", certificationNotPassedEnterpriseItemListMap.get("total"));

		return map;
	}
	
	/**
	 * 查询已通过认证审核的公司
	 * @param page 请求第几页  从0开始   
	 * @param size 每页显示数量
	 * @return status:error/success msg:错误或成功消息  certificationPassedEnterpriseItemList:已通过认证审核的公司ItemVO total:已通过认证审核的全部公司数量
	 */
	public Map<String, Object> findCertificationPassedEnterpriseItemVO(Integer page, Integer size){
		Map<String, Object> map = new HashMap<>();
		
		if(page == null || size == null){
			map.put("status", "error");
			map.put("msg", "分页为空");
			return map;
		}
		
		Map<String, Object> certificationPassedEnterpriseItemListMap = enterpriseDao.findCertificationPassedEnterpriseItemVO(page, size);
		map.put("status", "success");
		map.put("msg", "操作成功");
		map.put("certificationPassedEnterpriseItemList", certificationPassedEnterpriseItemListMap.get("certificationPassedEnterpriseItemList"));
		map.put("total", certificationPassedEnterpriseItemListMap.get("total"));

		return map;
	}
	
	/**
	 * 查询已提交认证，但待审核的公司
	 * @param page 请求第几页  从0开始   
	 * @param size 每页显示数量
	 * @return status:error/success msg:错误或成功消息  certificationWaitingEnterpriseItemList:已提交认证,但待审核的公司ItemVO total:已提交认证，但待审核的公司的全部公司数量
	 */
	public Map<String, Object> findCertificationWaitingEnterpriseItemVO(Integer page, Integer size){
		Map<String, Object> map = new HashMap<>();
		
		if(page == null || size == null){
			map.put("status", "error");
			map.put("msg", "分页为空");
			return map;
		}
		
		Map<String, Object> certificationWaitingEnterpriseItemListMap = enterpriseDao.findCertificationWaitingEnterpriseItemVO(page, size);
		map.put("status", "success");
		map.put("msg", "操作成功");
		map.put("certificationWaitingEnterpriseItemList", certificationWaitingEnterpriseItemListMap.get("certificationWaitingEnterpriseItemList"));
		map.put("total", certificationWaitingEnterpriseItemListMap.get("total"));

		return map;
	}
	
	/**
	 * 根据公司id,查询认证未通过公司详细信息
	 * @param enterpriseId 公司id
	 * @return status:error/success msg:错误或成功消息  detail:认证未通过公司详细信息VO
	 */
	public Map<String, Object> findCertificationNotPassedEnterpriseDetailVO(String enterpriseId){
		Map<String, Object> map = new HashMap<>();
		
		if(enterpriseId == null || enterpriseId.length() == 0){
			map.put("status", "error");
			map.put("msg", "公司为空");
			return map;
		}
		
		CertificationNotPassedEnterpriseDetailVO detail = enterpriseDao.findCertificationNotPassedEnterpriseDetailVO(enterpriseId);
		if(detail == null){
			map.put("status", "error");
			map.put("msg", "查无此公司");
			return map;
		}
		
		map.put("status", "success");
		map.put("msg", "操作成功");
		map.put("detail", detail);
		return map; 
	}
	
	/**
	 * 根据公司id,查询认证已通过公司详细信息
	 * @param enterpriseId 公司id
	 * @return status:error/success msg:错误或成功消息  detail:认证已通过公司详细信息VO
	 */
	public Map<String, Object> findCertificationPassedEnterpriseDetailVO(String enterpriseId){
		Map<String, Object> map = new HashMap<>();
		
		if(enterpriseId == null || enterpriseId.length() == 0){
			map.put("status", "error");
			map.put("msg", "公司为空");
			return map;
		}
		
		CertificationPassedEnterpriseDetailVO detail = enterpriseDao.findCertificationPassedEnterpriseDetailVO(enterpriseId);
		if(detail == null){
			map.put("status", "error");
			map.put("msg", "查无此公司");
			return map;
		}
		
		map.put("status", "success");
		map.put("msg", "操作成功");
		map.put("detail", detail);
		return map; 
	}
	
	/**
	 * 根据公司id,查询已提交认证,但待审核的公司详细信息
	 * @param enterpriseId 公司id
	 * @return status:error/success msg:错误或成功消息  detail:待审核公司详细信息VO
	 */	
	public Map<String, Object> findCertificationWaitingEnterpriseDetailVO(String enterpriseId){
		Map<String, Object> map = new HashMap<>();
		
		if(enterpriseId == null || enterpriseId.length() == 0){
			map.put("status", "error");
			map.put("msg", "公司为空");
			return map;
		}
		
		CertificationWaitingEnterpriseDetailVO detail = enterpriseDao.findCertificationWaitingEnterpriseDetailVO(enterpriseId);
		if(detail == null){
			map.put("status", "error");
			map.put("msg", "查无此公司");
			return map;
		}
		
		map.put("status", "success");
		map.put("msg", "操作成功");
		map.put("detail", detail);
		return map; 
	}
	
	/**
	 * 拒绝或同意公司认证申请
	 * @param enterpriseId 公司id
	 * @param status 公司状态 拒绝:3  同意:1
	 * @param refuseReason 若拒绝,则需输入拒绝原因
	 * @param userId 操作者id
	 * @return status:error/success msg:错误或成功消息
	 */
	public Map<String, Object> updateEnterpriseStatus(String enterpriseId, String status, String refuseReason, String userId){
		Map<String, Object> map = new HashMap<>();
		
		if(enterpriseId == null || enterpriseId.length() == 0){
			map.put("status", "error");
			map.put("msg", "公司为空");
			return map;
		}
		
		if(!"1".equals(status) && !"3".equals(status)){
			map.put("status", "error");
			map.put("msg", "公司状态有误");
			return map;
		}
		
		
		if("3".equals(status) && (refuseReason == null || refuseReason.length() == 0)){
			map.put("status", "error");
			map.put("msg", "未输入拒绝原因");
			return map;
		}
		
		if(!"2".equals(enterpriseRepository.findStatusByEnterpriseId(enterpriseId))){
			map.put("status", "error");
			map.put("msg", "公司现存状态有误");
			return map;
		}
		
		String backLogDetail = null;
		if("1".equals(status)){
			enterpriseRepository.updateEnterpriseStatusToOne(enterpriseId, new Date());
			backLogDetail = "同意id为" + enterpriseId + "的公司的认证申请。";
		}
		
		if("3".equals(status)){
			enterpriseRepository.updateEnterpriseStatusToThree(enterpriseId, refuseReason, new Date());
			backLogDetail = "拒绝了id为" + enterpriseId + "的公司的认证申请,拒绝原因为" + refuseReason;
		}
		
		backLogService.addBackLog(userId, backLogDetail);
		map.put("status", "success");
		map.put("msg", "操作成功");

		return map;
	}
	
}
